import { toRaw } from 'vue'
import { dataSave } from '../util/data.js'

const mutations = {
    //画布初始化,在主页面调用
    initSvg(state, svgOp) {
        state.svg = svgOp.svg
        //只在初始化添加一次svg
        dataSave('svg', svgOp.geo)
    },
    //删除选中
    deleteTarget(state) {
        if (state.targets.length > null) {

            for (let i = 0; i < state.targets.length; i++) {
                //在svg中删除
                let id = state.targets[i].id
                console.log('选中的要删除的id', id);
                for (let j = 0; j < state.svgData.length; j++) {
                    if (state.svgData[j].id === id) {
                        state.svgData.splice(j, 1)
                    }
                }
                //在targets中删除
                state.targets[i].remove()
            }
        }
        let targets = toRaw(state.targets)
        console.log('删除选中', targets);
    },
    //取消选中
    cancleTarget(state) {
        if (state.targets.length > 0) {
            for (const item of state.targets) {
                item.style("filter", "");
            }
            state.length = 0
        }
        let targets = toRaw(state.targets)
        console.log('取消选中', targets);
    },
    //数据保存
    dataSave(state, data) {
        state.svgData.push(data)
        let svgData = toRaw(state.svgData)
        console.log('画布数据', svgData);
    }
}

export default mutations

